﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BattleShipProject
{
	public class Ship
	{
		public Ship(ShipType st, ShipDirection d, Cell position)
		{
			ShipType = st;//Засунунть в проперти ШыпТайп значение полученое в ст
			Direction = d;
			Position = position;
		}

		public Cell Position { get; set; }

		public ShipType ShipType { get; private set; }//autoproperty
		public ShipDirection Direction { get; set; }

		public Cell[] ShipCells
		{
			get
			{
				Cell[] cells = new Cell[ShipSize];
				cells[0] = Position;
				for(int i = 1; i < cells.Length; i++)
				{
					Cell prev = cells[i - 1];
					if (Direction == ShipDirection.Horizontal)
					{
						cells[i] = new Cell(prev.X + 1, prev.Y);
					}
					else if(Direction == ShipDirection.Vertical)
					{
						cells[i] = new Cell(prev.X, prev.Y + 1);
					}
				}

				return cells;
			}
		}

		public Cell[] BlockedCells
		{
			get
			{
				List<Cell> cells = new List<Cell>();
				Cell[] shipCells = ShipCells;
				for(int i = 0; i < shipCells.Length; i++)
				{
					var c = shipCells[i];

					if(Direction == ShipDirection.Horizontal)
					{
						if(i == 0)
						{
							cells.Add(new Cell(c.X - 1, c.Y - 1));
							cells.Add(new Cell(c.X - 1, c.Y));
							cells.Add(new Cell(c.X - 1, c.Y + 1));
						}

						cells.Add(new Cell(c.X, c.Y - 1));
						cells.Add(new Cell(c.X, c.Y + 1));

						if (i == shipCells.Length - 1)
						{
							cells.Add(new Cell(c.X + 1, c.Y - 1));
							cells.Add(new Cell(c.X + 1, c.Y));
							cells.Add(new Cell(c.X + 1, c.Y + 1));
						}
					}
					else if(Direction == ShipDirection.Vertical)
					{
						if (i == 0)
						{
							cells.Add(new Cell(c.X - 1, c.Y - 1));
							cells.Add(new Cell(c.X, c.Y - 1));
							cells.Add(new Cell(c.X + 1, c.Y - 1));
						}

						cells.Add(new Cell(c.X - 1, c.Y));
						cells.Add(new Cell(c.X + 1, c.Y));

						if (i == shipCells.Length - 1)
						{
							cells.Add(new Cell(c.X - 1, c.Y + 1));
							cells.Add(new Cell(c.X, c.Y + 1));
							cells.Add(new Cell(c.X + 1, c.Y + 1));
						}
					}
				}

				return cells.ToArray();
			}
		}

		public int ShipSize
		{
			get
			{
				switch (ShipType)
				{
					case ShipType.AircraftCarrier:
						return 4;
					case ShipType.Destroyer:
						return 3;
					case ShipType.Fregate:
						return 2;
					case ShipType.Corvette:
						return 1;
					default:
						return 0;
				}
			}
		}
	}
}
